# -*- coding: utf-8 -*-
"""
Created on Thu Aug 25 10:27:22 2022

@author: xiong
"""

import pika
import logging
from TaskReleaser import TaskReleaser

#from testtest import TaskReleaser
logger = logging.getLogger("facebook_spider")


class MqConsumer():
    def __init__(self,username,password,host,port,virtual_host,queue):
        pika.PlainCredentials(username=username,password=password) 
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=host,port=port ,virtual_host = virtual_host))
        self.channel = self.connection.channel()
        #创建队列
        self.channel.queue_declare(queue=queue,passive=True)
        #确定监听队列 [auto_ack:默认应答]
        self.connection.process_data_events()
        self.TaskReleaser = TaskReleaser() 
        self.channel.basic_consume(queue=queue,on_message_callback=lambda ch,method,properties,body: self.TaskReleaser.receive_task(ch,method,properties,body)) #d means message body, a means channel
        logger.info('[*]:start listening and accepting---')
        
    def start_consuming(self):
        #监听到有key传入，调用get_data()爬取数据
        self.channel.start_consuming()
